<div style="padding: 2em;">
    <fieldset style="margin-bottom: 1em;">
        <legend>Поиск</legend>
        <div style="padding-left: 0.3em;"><?php echo $this->EditTypes() ?></div>
        <table>
            <tbody id="SearchForm"><?php
                    $this->data['noop'] = true;
                    for ($i = 0; $i < 4; ++$i) {
                        $this->data['num'] = $i;
                        $this->SearchElemScene();
                        $this->data['noop'] = false;
                    }
            ?></tbody>
        </table>
        <div style="padding: 0.6em;"><a href="?" id="AddElem">Добавить условие</a></div>
        <div style="margin: 0.4em;">Сортировать по <?php echo $this->SortField()->ToHtml(); ?>
        Число строк результата <select id="NumRows">
            <option value="5">5</option>
            <option value="10">10</option>
            <option value="20">20</option>
            <option value="40">40</option>
            <option value="100">100</option>
        </select></div>

        <div class="PageMenu" style="margin-top: 1em">
            <button id="FindEdit">Найти</button>
            <?php echo $this->ControllElements(); ?>
<!--            <button id="ClearSelectin">Очистить</button>
            <button id="GoToOrder">Оформить заказ</button>-->
        </div>

    </fieldset>

    <div>
        <?php echo $this->SearchResultSwitch()->ToHtml(); ?>
    </div>

    <script type="text/javascript" id="EditFindScript">
        jsun.run(function() {

        jsun("#FindEdit").jsun.event({ onclick: Search });
        jsun("#AddElem").jsun.event({ onclick: AddSearchElem });
        jsun("#ClearSelectin") && jsun("#ClearSelectin").jsun.event({ onclick: ClearSelectin });
        jsun("#GoToOrder") && jsun("#GoToOrder").jsun.event({ onclick: GoToOrder });
        jsun.swap.EditSearch = { Num: 4 };

        function ClearSelectin() {
            jsun.ajax.send_request({ asyn: false, param: "u=Index/ClearItems" });
            if (!jsun("#EditList")) return;

            var list = jsun("#EditList").firstChild.childNodes;
            for (var i = 1; i < list.length; ++i) {
                list[i].lastChild.firstChild.checked = false;
            }
        }

        function GetSelItems() {
            var arr = [];
            var items = jsun.swap.SEARCH_DATA.SELECT_ITEM;
            for (var i in items)
                if (items[i]) arr.push(i);
            return arr;
        }

        function Search() {
            var form_elem = jsun("#SearchForm").childNodes,
                type = "&type=" + jsun("#EditType").value,
                sort = "&sort=" + jsun("#SortField").value,
                num = "&num=" + jsun("#NumRows").value,
                values, fields, ops;

            for (var i = 0; i < form_elem.length; ++i) {
                var ch = form_elem[i].childNodes;

                if (i > 0) {
                    ops += "," + ch[0].firstChild.value;
                    fields += "," + ch[1].firstChild.value;
                    values += "[^&]" + ch[2].firstChild.value;
                }
                else {
                    ops = "AND";
                    fields = ch[1].firstChild.value;
                    values = ch[2].firstChild.value;
                }
            }
            var param = "u=Index/Search" + type + sort + num
                + "&fields=" + encodeURIComponent(fields)
                + "&values=" + encodeURIComponent(values)
                + "&ops=" + encodeURIComponent(ops);

            jsun.json.load(function(p) {
                if (p.STAT == 'OK') {
                     var pref = "SearchResult",
                        param = "u=Index&m=Index\\Search/SearchResult&id="
                            + p.ID
                            + '&type=' + type
                            + '&num=' + num
                            + '&sort=' + sort;

                    jsun.swap[pref].Page = 1;
                    jsun.swap[pref].Req = param;
                    jsun.swap[pref].Start();
                } else {
                    jsun.dialog.warning('Ошибка', '')
                }
            }, param);
        }

        function AddSearchElem() {
            jsun.ajax.send_request({ ready: true,
                param: "u=Index&m=Index\\Search/SearchElem&json=1&clear=1&num="
                    + jsun.swap.EditSearch.Num,
                handler: function(req) {
                    var row = jsun.dom(jsun("!" + req.responseText));
                    jsun("#SearchForm").appendChild(row);
                    jsun.dom.mix(row.lastChild.firstChild).event({ onclick: DelSearchElem });
                    ++jsun.swap.EditSearch.Num;
                }
            });
        }

        function DelSearchElem() {
            var row = this.parentNode.parentNode;
            row.parentNode.removeChild(row);
            --jsun.swap.EditSearch.Num
        }

        function Dict() {

        }

        function GoToOrder() {
                window.location = "?u=Reader&p=Reader\\Order/Add";
            }

        });
    </script>
</div>